package com.shutong.springboot;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import java.util.Map;

public class ShuTongSpringApplication {
    public static void run(Class clazz){
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(clazz);
        context.refresh();
        // 获取tomcat、jetty任意一个启动容器
        WebServer webServer = getWebServer(context);
        webServer.start();
    }
    private static WebServer getWebServer(WebApplicationContext applicationContext){
        Map<String, WebServer> beansOfType = applicationContext.getBeansOfType(WebServer.class);
        if (beansOfType.isEmpty()) {
            throw new RuntimeException("at last one webServer");
        }
        if (beansOfType.size() != 1) {
            throw new RuntimeException("at most one webServer");
        }
        return beansOfType.values().stream().findFirst().get();
    }
}
